package com.gxs.ea.fileupload.beans.jsf.company;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import com.gxs.ea.fileportal.domain.BaseCompany;
import com.gxs.ea.fileupload.beans.data.service.CompanyService;

@Component
@Scope("session")
public class ViewAllCompaniesBean  implements Serializable
{
	private BaseCompany selectedCompany;
	private boolean viewParameterInit = false;
	
	@Autowired
	private CompanyService companyService;
	
	private List<BaseCompany> listOfCompanies = null;
	
	private void clearFields()
	{
		this.selectedCompany = null;
	}
	
	public void setViewParameterInit(boolean value)
	{
		this.viewParameterInit = value;
		if(value)
		{
			this.initialize();
		}
		
		Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> " + authorities);
	}

	
	public List<BaseCompany> getListOfCompanies() {
		return listOfCompanies;
	}

	public void setListOfCompanies(List<BaseCompany> listOfCompanies) {
		this.listOfCompanies = listOfCompanies;
	}

	public BaseCompany getSelectedCompany() {
		return selectedCompany;
	}

	public void setSelectedCompany(BaseCompany selectedCompany) {
		this.selectedCompany = selectedCompany;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public boolean isViewParameterInit() {
		return viewParameterInit;
	}
	
	public void fetchAll()
	{
		try
		{
			listOfCompanies = this.companyService.listCompanies();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Search Failed", "Error: " + e.getMessage()));
		}
	}
	
	private void initialize()
	{
		this.selectedCompany = null;
		this.listOfCompanies = null;
	}
}
